package com.sky.chapter1.group;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author : wushikai
 * <p>
 * date : 2022-04-08
 *
 * 线程组demo
 */
public class GroupDemo {


    public static void createGroup() {

        //获取当前线程的group
        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
        System.out.println("currentGroup = " + currentGroup);
        //在当前线程执行流中新建一个Group1
        ThreadGroup group1 = new ThreadGroup("Group1");
        //Group1的父线程,就是main线程所在Group
        System.out.println("group1.getParent() == currentGroup  ==> " + ( group1.getParent() == currentGroup  )  );
        //定义Group2, 指定group1为其父线程
        ThreadGroup group2 = new ThreadGroup(group1, "Group2");
        System.out.println(group2.getParent() == group1);

    }





    public static void destroyTest() {
        ThreadGroup group = new ThreadGroup("TestGroup");
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
        //before destroy
        System.out.println("group.isDestroyed=" + group.isDestroyed());
        mainGroup.list();

        group.destroy();
        //after destroy
        System.out.println("group.isDestroyed=" + group.isDestroyed());
        mainGroup.list();

    }


    public static void main(String[] args) {
        createGroup();
        //test1();
    }


    public static void test1() {


        int num = 10;
        ThreadGroup group = new ThreadGroup("test-group") {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("ThreadGroup捕获到线程异常 - " + e.getMessage());
            }
        };


        group.setDaemon(false);


        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Thread thread = new Thread(group, new MyThread(), "threadName-" + i);
            threadList.add(thread);
        }

        System.out.println("运行前线程组中活跃线程数 -> " + group.activeCount());
        System.out.println("开始运行所有线程...");
        for (Thread t : threadList) {
            t.start();
        }
//获取线程组中所有[活动]线程
    /*    Thread[] threads = new Thread[num];
        testGroup.enumerate(threads);
        for (Thread t : threads) {
            System.out.println("线程名-" + t.getName());
        }*/
        System.out.println("所有线程运行后,线程组中活跃线程数->" + group.activeCount());
//不断的查看线程组中活跃的线程数
        Thread thread = new Thread(() -> {
            int num1;
            try {
                while ((num1 = group.activeCount()) > 0) {
                    System.out.println("当前线程组活跃线程数为 -> " + num1);
                    TimeUnit.SECONDS.sleep(1);
                }
                System.out.println("All Thread HAS FINISHED");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.start();




    }


    private static class MyThread extends Thread{


        @Override
        public void run() {
            System.out.println("my thread  run....");
        }
    }


}
